﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    // NOTE: If you change the class name "ServiceProviders" here, you must also update the reference to "ServiceProviders" in App.config.
    public class ServiceProviders : IServiceProviders
    {
       
        #region IServiceProviders Members


        public void AddNewCategory(Category category)
        {
            LinQProviders.linqProvider.SPAddnewCategory(category.NAME, category.SORT);
            LinQProviders.linqProvider.SubmitChanges();
        }


       


        public void DeleteCategory(int id)
        {
            LinQProviders.linqProvider.SPDeleteCategory(id);
            LinQProviders.linqProvider.SubmitChanges();
        }

        public void UpdateCategory(Category category)
        {
            LinQProviders.linqProvider.SPUpdateCategory(category.ID, category.NAME, category.SORT);
            LinQProviders.linqProvider.SubmitChanges();
        }

      
        public void AddNewProduct(Product product)
        {
            LinQProviders.linqProvider.SPAddNewProduct(product.CategoryId, product.Name, product.Picture, product.Price, product.Desc, product.Quantity);
            LinQProviders.linqProvider.SubmitChanges();
        }
        public void UpdateProduct(Product product)
        {
            LinQProviders.linqProvider.SPUpdateProduct(product.Id, product.CategoryId, product.Name, product.Picture, product.Price, product.Desc, product.Quantity);
            LinQProviders.linqProvider.SubmitChanges();

        }
        
        public void DeleteProduct(int ProductId)
        {

            LinQProviders.linqProvider.SPDeleteProduct(ProductId);
            LinQProviders.linqProvider.SubmitChanges();

        }

       


        public List<Product> GetAllNewProduct()
        {
           
            var result = from item in LinQProviders.linqProvider.tbl_Products
                         select new Product
                         {
                             Id = item.Id,
                             Name = item.Name,
                             CategoryId = item.CategoryId,
                             Picture = item.Images.ToArray(),
                             Price = float.Parse(item.Price.ToString()),
                             Desc = item.Summary,
                             Quantity = int.Parse(item.Quantity.ToString())

                         };
            return result.ToList();
        }


     


        public List<Product> GetProductBelongtoCategory(int categoryId)
        {

            var result = from item in LinQProviders.linqProvider.tbl_Products
                         where item.CategoryId==categoryId
                         select new Product
                         {
                             Id = item.Id,
                             Name = item.Name,
                             CategoryId = item.CategoryId,
                             Picture = item.Images.ToArray(),
                             Price = float.Parse(item.Price.ToString()),
                             Desc = item.Summary,
                             Quantity = int.Parse(item.Quantity.ToString())

                         };
            return result.ToList();
        }

        public byte[] GetImages(int productId)
        {
            var item = from i in LinQProviders.linqProvider.tbl_Products
                       where productId == i.Id
                       select i;
            List<tbl_Product> list = item.ToList<tbl_Product>();

            byte[] imgData = (byte[])((list[0].Images).ToArray());
            return imgData;
        }

        public Product GetDetailProduct(int id)
        {
           Product product = new Product();
           var result = from item in LinQProviders.linqProvider.tbl_Products
                        where item.Id == id
                        select new Product
                        {
                            Id = item.Id,
                            Name = item.Name,
                            CategoryId = item.CategoryId,
                            Picture = item.Images.ToArray(),
                            Price = float.Parse(item.Price.ToString()),
                            Desc = item.Summary,
                            Quantity = int.Parse(item.Quantity.ToString())

                        };
           List<Product> list = result.ToList<Product>();
            return list[0];
        }

        #endregion





        #region IServiceProviders Members

        public List<Category> GetAllCategorys()
        {
            var result = from i in LinQProviders.linqProvider.SPGetCategorys()
                         select i;
            List<Category> list = new List<Category>();
            foreach (var item in result)
            {
                Category category = new Category();
                category.ID = item.Id;
                category.NAME = item.Name;
                category.SORT = item.Sort.Value;
                list.Add(category);
            }
            return list;
        }

        #endregion
    }
}
